/* eslint-disable */
import * as modelCreateService from '../../services/synergy/modelCreateService';
import { message } from 'antd';
import config from '../../config/config';
import { valueToPercent } from '../../utils/commonUtil';

export default {
  namespace: 'modelCreateModel',
  state: {
    modelCreateList: [],
    total: 0,
    modelCreateLoading: false,
    currentPage: 0,
    pageSize: 0,
    modelCreateModalVisible: true,
    searchParams: null,
    isExpandSearch: false,
    dimensionList: [],
    applicationAreaList: [],
    industryList: [],
    selectedItemRowKeys: [],
    selectedItemRows: [],
    current: 0,
    modelId: null,
    selectedRowKeys: [],
    selected: [],
    list: [],
    selectedStringList: [],
    levestList: [],
    levestListClone: [],
    radioFlag: 1,
  },

  reducers: {
    updateState(state, { payload }) {
      return { ...state, ...payload };
    },
  },

  effects: {
    *init({ payload: params }, { select, call, put }) {
      yield put({ type: "getListDatas", payload: {} });
      yield put({ type: "getAllDictionaryValueNotPage", payload: "SIC-2 Industry" });
      yield put({ type: "getAllDictionaryValueNotPage", payload: "model_function" });
      yield put({ type: "getAllDictionaryValueNotPage", payload: "information_dimension" });
    },
    *getListDatas({ payload: { currentPage = 1, pageSize = config.PAGE_SIZE, ...params }, }, { call, put },) {
      yield put({ type: 'updateState', payload: { modelCreateLoading: true } });
      const res = yield call(modelCreateService.getPoolList, { ...params, currentPage, pageSize, status: "true" });
      if (res.code == '200') {
        yield put({
          type: 'updateState',
          payload: {
            modelCreateList: res.data.records,
            total: res.data.total,
            currentPage,
            pageSize,
          },
        });
      }
      yield put({ type: 'updateState', payload: { modelCreateLoading: false } });
    },
    // 下拉列表
    *getAllDictionaryValueNotPage({ payload: params }, { select, call, put }) {
      const res = yield call(modelCreateService.getAllDictionaryValueNotPage, { dictCode: params });
      if (res.code == 200) {
        switch (params) {
          case 'information_dimension':
            yield put({ type: 'updateState', payload: { dimensionList: res.data, }, }); //信息维度
            break;
          case 'model_function':
            yield put({ type: 'updateState', payload: { applicationAreaList: res.data, }, }); //模型功能
            break;
          case 'SIC-2 Industry':
            yield put({ type: 'updateState', payload: { industryList: res.data, }, }); //所属行业
            break;
          // no default
        }
      }
    },
    *getByCode({ payload: params }, { select, call, put }) {
      const res = yield call(modelCreateService.getByCode, params);
      if (res.code == 200) {
        yield put({ type: 'updateState', payload: { selectedStringList: res.data } });
      }
    },

    *addModel({ payload: params }, { put, call }) {
      const res = yield call(modelCreateService.addModel, params);
      if (res.code == '200') {
        message.info('新增成功！');
        yield put({ type: 'updateState', payload: { modelCreateModalVisible: false } });
        yield put({ type: 'updateState', payload: { modelId: res.data } });
      } else if (res.code == '400') {
        message.info(res.message);
      } else {
        message.info('新增失败！');
      }
    },

    *addScoreRule({ payload: { params, callk } }, { put, call }) {
      const res = yield call(modelCreateService.addScoreRule, params);
      if (res.code == '200') {
        message.info('新增成功！');
        yield put({ type: 'updateState', payload: { current: 2 } });
        callk && callk()
      } else {
        message.info('新增失败！');
      }
    },

    *insertModelIndicatorPool({ payload: { params, list } }, { select, put, call }) {
      const res = yield call(modelCreateService.insertModelIndicatorPool, params);
      if (res.code == '200') {
        message.info('新增成功！');
        let listOder = params.indicatorPoolModelList;
        let listClone = listOder.map(element => {
          let data = list.filter(item => item.indicatorId == element.pid)
          if (data.length > 0) {
            element = data[0]
          } else {
            element.selectedList = []
            element.indicatorId = element.pid
            if (!(element.ruleList && element.ruleList.length > 0)) {
              element.ruleList = [
                {
                  indicatorId: element.pid,
                  weight: '',
                  zId: element.pid,
                  zType: element.type,
                  score: '0',
                  ifNullRule: '0',
                  otherScore: '0',
                  pid: "item000"
                },
              ];
            }
          }
          return element
        });
        yield put({ type: 'updateState', payload: { current: 1, list:listClone } });
      } else {
        message.info('新增失败！');
      }
    },

    *indicatorRuleAdd({ payload: { indicatorRules, modelLogicalRuleScoreList, callk } }, { put, call }) {
      const res = yield call(modelCreateService.indicatorRuleAdd, { indicatorRules });
      if (res.code == '200') {
        yield put({ type: 'addScoreRule', payload: { params: { modelLogicalRuleScoreList }, callk } });
      }
    },

    *indicatorRuleGetList({ payload: params }, { put, call }) {
      const res = yield call(modelCreateService.indicatorRuleGetList, params);
      if (res.code == '200') {
        let data = res.data.length > 0 ? res.data[0].indicatorRules : []
        let pnameList = ""
        data.map(item => {
          item.pid = item.indicatorId
          item.pname = item.indicatorName
          item.type = item.indicatorType
          pnameList += item.category + ","
          item.weight = valueToPercent(item.weight)
          item.selectedList = []
          item.ruleList = item.rules.map((i, index) => {
            i.zId = item.indicatorId
            i.zType = item.indicatorType
            i.pid = i.ifNullRule == 0 ? "item000" : "item" + item.indicatorId
            return i
          })
          if (item.type == "字符型") {
            item.ruleList.push({
              indicatorId: item.rules[0].indicatorId,
              weight: '',
              zId: item.rules[0].indicatorId,
              zType: item.rules[0].indicatorType,
              score: '0',
              ifNullRule: '1',
              otherScore: item.rules[0].otherScore,
              pid: "itemOther000"
            })
          }
          return item
        })
        yield put({ type: 'updateState', payload: { list: data } });
        yield put({ type: 'getByCode', payload: { dictValue: pnameList } });
      }
    },

    *getScoreRuleList({ payload: params }, { put, call }) {
      const res = yield call(modelCreateService.getScoreRuleList, params);
      if (res.code == '200') {
        yield put({ type: 'updateState', payload: { levestList: res.data } });
      }
    },
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(location => {
        if (location.pathname == "/scmp/synergy/modelCreate") {
          dispatch({ type: 'init', payload: {} });
        }
      });
    },
  },
};
